/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBean;

import entity.UnstructuredContentElementEntity;
import entity.ImageElementEntity;
import entity.TextElementEntity;
import entity.UnstructuredContentPanelEntity;
import entity.VideoElementEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import org.primefaces.event.DragDropEvent;
import util.enumeration.PanelContent;

/**
 *
 * @author WenYong
 */
@ManagedBean(name = "addUnstructuredContentTemplateElementsManager")
@RequestScoped
public class AddUnstructuredContentTemplateElementsManager {

    private String template;
    private SelectItem[] noOfPanels = new SelectItem[5];
    private SelectItem[] twoPanels = new SelectItem[2];
    private SelectItem[] threePanels = new SelectItem[3];
    private PanelContent panelSelected;
    private String imageofTemplate;
    private String templateSelected;
    private static int noOfTextElement;
    private static int noOfImgElement;
    private static int noOfVidElement;
    private static int totalElements;
    private String inputValue;
    private String id;
    private static List<UnstructuredContentElementEntity> selectedList;
    private List<UnstructuredContentElementEntity> selected;
    private static List<UnstructuredContentPanelEntity> templatePanels = new ArrayList<UnstructuredContentPanelEntity>();
    private static UnstructuredContentElementEntity selectedElement;
    private static UnstructuredContentPanelEntity selectedPanel;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private static boolean panelOneAdded;
    private static boolean panelTwoAdded;
    private static boolean panelThreeAdded;

    public AddUnstructuredContentTemplateElementsManager() {
        if (selectedList == null) {
            selectedList = new ArrayList<UnstructuredContentElementEntity>();
        }
    }

    public void initView(PhaseEvent event) {

        template = ctx.getExternalContext().getSessionMap().get("TemplateSelected").toString();
        templateSelected = "You have selected Template Number:" + template;
        imageofTemplate = "images\\Template" + getTemplate() + ".png";

        PanelContent[] panelContent = PanelContent.values();
        twoPanels[0] = new SelectItem(panelContent[0]);
        twoPanels[1] = new SelectItem(panelContent[1]);
        threePanels[0] = new SelectItem(panelContent[0]);
        threePanels[1] = new SelectItem(panelContent[1]);
        threePanels[2] = new SelectItem(panelContent[2]);
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
    }

    public SelectItem[] getNoOfPanels() {
        String temp = getTemplate();
        System.out.println("Template: " + temp);
        if ((temp.equalsIgnoreCase("1")) || (temp.equalsIgnoreCase("2"))) {
            noOfPanels = twoPanels;
        } else if ((temp.equalsIgnoreCase("3")) || (temp.equalsIgnoreCase("4")) || (temp.equalsIgnoreCase("5")) || (temp.equalsIgnoreCase("6"))) {
            noOfPanels = threePanels;
        }
        return noOfPanels;
    }

    public void setNoOfPanels(SelectItem[] noOfPanels) {
        this.noOfPanels = noOfPanels;
    }

    public PanelContent getPanelSelected() {
        return panelSelected;
    }

    public void setPanelSelected(PanelContent panelSelected) {
        this.panelSelected = panelSelected;
    }

    public String getImageofTemplate() {
        return imageofTemplate;
    }

    public void setImageofTemplate(String imageofTemplate) {
        this.imageofTemplate = imageofTemplate;
    }

    public String getTemplateSelected() {
        return templateSelected;
    }

    public void setTemplateSelected(String templateSelected) {
        this.templateSelected = templateSelected;
    }

    public List<UnstructuredContentElementEntity> getSelectedList() {
        return selectedList;
    }

    public UnstructuredContentPanelEntity getSelectedPanel() {
        return selectedPanel;
    }

    public void setSelectedPanel(UnstructuredContentPanelEntity selectedPanel) {
        this.selectedPanel = selectedPanel;
    }

    public void delete() {
        templatePanels.remove(selectedPanel);
        if(panelSelected.toString().equalsIgnoreCase("CONTENT1"))
        {
            panelOneAdded = false;
        }
        else if(panelSelected.toString().equalsIgnoreCase("CONTENT2"))
        {
            panelTwoAdded = false;
        }
         else if(panelSelected.toString().equalsIgnoreCase("CONTENT3"))
        {
            panelThreeAdded = false;
        }
    }

    public UnstructuredContentElementEntity getSelectedElement() {
        System.out.println("Selected Element Name: " + selectedElement.getName());
        return selectedElement;
    }

    public void setSelectedElement(UnstructuredContentElementEntity selectedElement) {
        this.selectedElement = selectedElement;
    }

    public void deleteElements() {
        selectedList.remove(getSelectedElement());
    }

    public void setSelectedList(List<UnstructuredContentElementEntity> selectedList) {
        this.selectedList = selectedList;
    }

    public List<UnstructuredContentPanelEntity> getTemplatePanels() {
        return templatePanels;
    }

    public void setTemplatePanels(List<UnstructuredContentPanelEntity> aTemplatePanels) {
        setTemplatePanels(aTemplatePanels);
    }

    public List<UnstructuredContentElementEntity> getSelected() {
        return selected;
    }

    public void setSelected(List<UnstructuredContentElementEntity> selected) {
        this.selected = selected;
    }

    public void addPanelComponents(ActionEvent event) {
        if (panelSelected.toString().equalsIgnoreCase("CONTENT1") && panelOneAdded == false) {
            panelOneAdded = true;
            List<UnstructuredContentElementEntity> tempList = new ArrayList<UnstructuredContentElementEntity>();
            System.out.println("Element Size Added: " + tempList.size());
            UnstructuredContentPanelEntity panel = new UnstructuredContentPanelEntity();
            panel.setPanelName(panelSelected.toString());
            for (int j = 0; j < selectedList.size(); j++) {
                tempList.add(selectedList.get(j));
            }
            panel.setUnstructuredContentElement(tempList);
            templatePanels.add(panel);
            resetValues();
        } else if (panelSelected.toString().equalsIgnoreCase("CONTENT2") && panelTwoAdded == false) {
            panelTwoAdded = true;
            List<UnstructuredContentElementEntity> tempList = new ArrayList<UnstructuredContentElementEntity>();
            System.out.println("Element Size Added: " + tempList.size());
            UnstructuredContentPanelEntity panel = new UnstructuredContentPanelEntity();
            panel.setPanelName(panelSelected.toString());
            for (int j = 0; j < selectedList.size(); j++) {
                tempList.add(selectedList.get(j));
            }
            panel.setUnstructuredContentElement(tempList);
            templatePanels.add(panel);
            resetValues();
        } else if (panelSelected.toString().equalsIgnoreCase("CONTENT3") && panelThreeAdded == false) {
            panelThreeAdded = true;
            List<UnstructuredContentElementEntity> tempList = new ArrayList<UnstructuredContentElementEntity>();
            System.out.println("Element Size Added: " + tempList.size());
            UnstructuredContentPanelEntity panel = new UnstructuredContentPanelEntity();
            panel.setPanelName(panelSelected.toString());
            for (int j = 0; j < selectedList.size(); j++) {
                tempList.add(selectedList.get(j));
            }
            panel.setUnstructuredContentElement(tempList);
            templatePanels.add(panel);
            resetValues();
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The panel has been added previously. Please delete it before you can get it again."));
            resetValues();
        }
    }

    public void addPanelDetails(ActionEvent event) {

        try {
            ctx.getExternalContext().getSessionMap().put("templatePanels", getTemplatePanels());
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            if (template.equalsIgnoreCase("1")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateOne.xhtml");
            } else if (template.equalsIgnoreCase("2")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateTwo.xhtml");
            } else if (template.equalsIgnoreCase("3")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateThree.xhtml");
            } else if (template.equalsIgnoreCase("4")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFour.xhtml");
            } else if (template.equalsIgnoreCase("5")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFive.xhtml");
            } else if (template.equalsIgnoreCase("6")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateSix.xhtml");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void elementsDropped(DragDropEvent event) throws IOException {
        id = event.getDragId();
        totalElements++;
        int temp2 = totalElements;
        if (id.endsWith("txt1")) {
            noOfTextElement++;
            inputValue = "One Text Element has been chosen";
            int temp = noOfTextElement;
            TextElementEntity c = new TextElementEntity(temp2, "txt" + temp, "");
            selectedList.add(c);
        } else if (id.endsWith("img")) {
            int temp = noOfImgElement;
            noOfImgElement++;
            inputValue = "One Image Elemnt has been chosen";
            ImageElementEntity r = new ImageElementEntity(temp2, "img" + temp, "");
            selectedList.add(r);
        } else if (id.endsWith("vid")) {
            int temp = noOfVidElement;
            noOfVidElement++;
            inputValue = "One Video Elemnt has been chosen";
            VideoElementEntity v = new VideoElementEntity(temp2, "vid" + temp, "");
            selectedList.add(v);
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(inputValue));
    }

    public void resetValues() {
        noOfTextElement = 0;
        noOfImgElement = 0;
        noOfVidElement = 0;
        totalElements = 0;
        selectedList.clear();
    }
}